package com.fau.wm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalRecallDetail;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
/**
 * @desc 出库详细action
 * @author kangyj
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class RecallDetailAction extends PublicAction implements ModelDriven<MedicalRecallDetail> {

	private static final long serialVersionUID = -2296935039323220039L;
	
	private MedicalRecallDetail recallDetail = new MedicalRecallDetail();

	/**
	 * @desc 初始化出库详细页面
	 * @return String
	 * @author kangyj
	 * @datetime 2014年8月12日,下午5:23:44
	 */
	@Action(value = "recall_detail_init", results = {@Result(name = SUCCESS, location = "recall_detail_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询出库详细列表
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月12日,下午5:23:13
	 */
	@Action(value = "recall_detail_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		list = this.baseAllService.getRecallManagerService().findRecallDetailList(key);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询包含特定药品的出库单
	 * @return null
	 * @author liy
	 * @datetime 2014年12月20日,下午5:44:07
	 */
	@Action(value = "recall_medicine_detail")
	public String recallMedicineDetail(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		if(StringUtils.isNotBlank(this.recallDetail.getMedicineId()) 
				&& StringUtils.isNotBlank(this.recallDetail.getWarehouseId())){
			this.pageBean = this.baseAllService.getRecallManagerService().findMedicineRecallDetailList(recallDetail.getMedicineId(),recallDetail.getWarehouseId(),this.map);
			this.map.clear();
			map.put("total", this.pageBean.getTotalRow());
			map.put("data", this.pageBean.getResult());
			formatJSON(response, this.map);
		}
		return null;
	}
	
	/**
	 * @desc 添加出库详细
	 * @author kangyj
	 * @datetime 2014年8月12日,下午5:24:19
	 */
	@Action(value = "recall_detail_add", results = {@Result(name = SUCCESS, location = "recall_detail_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存出库详细
	 * @return String
	 * @author kangyj
	 * @datetime 2014年8月12日,下午5:32:16
	 */
	@Action(value = "recall_detail_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		Boolean flag = this.baseAllService.getRecallManagerService().saveRecallDetail(recallDetail);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 编辑出库详细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月12日,下午5:33:10
	 */
	@Action(value = "recall_detail_edit", results = {@Result(name = SUCCESS, location = "recall_detail_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑入库明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月12日,下午5:33:41
	 */
	@Action(value = "recall_detail_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(recallDetail.getId())){
			this.baseAllService.getRecallManagerService().saveEditRecallDetail(recallDetail);
			map.put("success", true);
		}else{
			map.put("success", false);
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除入库明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月12日,下午6:39:33
	 */
	@Action(value = "recall_detail_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(recallDetail.getId())){
			this.baseAllService.getRecallManagerService().delRecallDetail(recallDetail);
			map.put("success", true);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 按名称模糊查询药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月4日,下午8:40:06
	 */
	@Action(value = "recall_detail_find_medicine")
	public String findMedicineByName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMedicineService().findMedicineByName(admin, key);
		formatJSON(response, list);
		return null;
	}

	/**
	 * @desc 检查出库明细表中是否已存在将要添加的药品
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月8日,下午2:44:45
	 */
	@Action(value = "recall_detail_check_medicine")
	public String checkMedicineExist(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(recallDetail.getRecallId()) && StringUtils.isNotBlank(recallDetail.getMedicineId())){
			Boolean flag = this.baseAllService.getRecallManagerService().checkMedicineExist(recallDetail.getRecallId(), recallDetail.getMedicineId());
			if(flag){
				map.put("able", true);
			}else{
				map.put("able", false);
			}
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 修改时判断出库明细表中是否已存在重复的药品
	 * @return null
	 * @author liy
	 * @datetime 2014年12月19日,下午3:07:56
	 */
	@Action(value = "recall_detail_check_medicine_e")
	public String checkMedicineExistE(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(recallDetail.getRecallId()) && StringUtils.isNotBlank(recallDetail.getMedicineId())){
			Boolean flag = this.baseAllService.getRecallManagerService().checkMedicineExistE(recallDetail.getRecallId(), recallDetail.getMedicineId());
			if(flag){
				map.put("able", true);
			}else{
				map.put("able", false);
			}
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 检查将要添加的药品库存量是否充足
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月14日,下午2:44:48
	 */
	@Action(value = "recall_detail_check_inventory")
	public String checkMedicineInventory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(recallDetail.getWarehouseId())
				&& StringUtils.isNotBlank(recallDetail.getMedicineId()) 
				&& recallDetail.getQuantity() != null){
			Boolean flag = this.baseAllService.getInStockManagerService().checkMedicineInStock(recallDetail.getMedicineId(), recallDetail.getWarehouseId(), recallDetail.getQuantity());
			if(flag){
				map.put("able", true);
			}else{
				map.put("able", false);
			}
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 查询出库明细对象
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月25日,下午3:56:54
	 */
	@Action(value = "recall_detail_find")
	public String findRecallDetail(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(recallDetail.getId())){
			recallDetail = this.baseAllService.getRecallManagerService().findRecallDetailById(recallDetail.getId());
			map.put("recallDetail", recallDetail);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 出库验证产品是否存在仓库中
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月10日,下午7:39:37
	 */
	@Action(value = "recall_valid_pro")
	public String validProExist() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		String errorInfo = this.baseAllService.getRecallManagerService().validProExist(recallDetail);
		map.put("errorInfo", errorInfo);
		formatJSON(response, map);
		return null;
	}
	
	
	public MedicalRecallDetail getRecallDetail() {
		return recallDetail;
	}

	public void setRecallDetail(MedicalRecallDetail recallDetail) {
		this.recallDetail = recallDetail;
	}

	@Override
	public MedicalRecallDetail getModel() {
		return recallDetail;
	}
	
	
}
